//
//  JHSFollowDoctorController.m
//  JHSSweep
//
//  Created by Apple on 2017/3/24.
//  Copyright © 2017年 JHDS. All rights reserved.
//


#import "JHSFollowDoctorController.h"
#import "JHSFollowDoctorModel.h"
#import "JHSDoctorListCell.h"
#import <MJRefresh.h>
#import "JHSDetailDoctorViewController.h"
#import "JHSDoctorApplyViewController.h"
#import "JHSNetworkTool.h"
#import "JHSFollowDoctorCell.h"


@interface JHSFollowDoctorController ()<UITableViewDataSource>

@property(strong,nonatomic)NSArray<JHSFollowDoctorModel *> *docList;// 医生列表模型数组

/**
 医生详情模型数组
 */
@property(strong,nonatomic)NSArray<JHSDocDetailModel *> *details;

/**
 医生简介模型数组
 */
@property(strong,nonatomic)NSArray<JHSDocIntrodunceModel *> *intros;



@property(weak,nonatomic)UILabel *codition;// 接诊条件
@property(weak,nonatomic)UILabel *introduce;// 医生简介
@property(assign,nonatomic)BOOL isSelect;// 是否选用


/**
 下拉刷新控件
 */
@property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader;
/**
 上拉刷新控件
 */
@property (nonatomic, strong) MJRefreshAutoNormalFooter *refreshFooter;
@end


static NSString *CellIdentifier = @"Cell";

@implementation JHSFollowDoctorController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];

    
    [self.tableView registerNib:[UINib nibWithNibName:@"JHSFollowDoctorCell" bundle:nil] forCellReuseIdentifier:@"123"];
    
    //设置返回按钮为白色
     self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
   
    //设置行高
    self.tableView.rowHeight = 80;
    
    //MARK: 设置上下拉刷新
    //解决循环引用问题
    __weak typeof(self) weakSelf = self;
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //刷新指定时间
        [NSThread sleepForTimeInterval:2.0];
        [weakSelf.tableView.mj_header endRefreshing];
        [JHSFollowDoctorController jhsShowSVSuccessTitle:@"加载完成" andTime:0.8];
    }];
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //刷新指定时间
        [NSThread sleepForTimeInterval:2.0];
        [weakSelf.tableView.mj_footer endRefreshing];
        [JHSFollowDoctorController jhsShowSVSuccessTitle:@"加载完成" andTime:0.8];
    }];
    
    
}

#pragma mark - 获取网络数据

-(void)loadData{
    //获取医生数据列表
    NSString *url;
    NSDictionary *parameter;

    url = @"http://iosapi.itcast.cn/doctorList.json.php";
    parameter = @{@"user_id":@1000089,@"page_size":@15,@"page":@1};
    
    
    [JHSFollowDoctorModel followDoctorNetDataWithURL:url withParameters:parameter withSuccessBlock:^(id responceBoby) {
        
        NSLog(@"%@",responceBoby);
        
        _docList=responceBoby;//数据列表保存
        [self.tableView reloadData];
        
    } withFailureBlock:^(NSError *error) {
        
        
    }];
     
}

#pragma mark - 代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _docModel=[JHSFollowDoctorModel yy_modelWithJSON:self.docList[indexPath.row]];
   UIStoryboard *sb = [UIStoryboard storyboardWithName:@"JHSDoctorDetailsStryboard" bundle:nil];
    JHSDetailDoctorViewController *follow = [sb instantiateInitialViewController];
    follow.docModel=self.docModel;
    
    //点击进行Push
    [self.navigationController pushViewController:follow animated:YES];
    
    //设置返回按钮的文字
    self.navigationItem.backBarButtonItem.title = @"返回";
    
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.docList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    JHSFollowDoctorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
    JHSFollowDoctorModel *model=[JHSFollowDoctorModel yy_modelWithJSON:self.docList[indexPath.row]];
    cell.docModel = model;
    
    return cell;
}



//刷新  docModel
+ (void)jhsShowSVSuccessTitle:(NSString *)content andTime:(float)delay{
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD setBackgroundColor:JHSMainColor];
    [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@",content]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}


@end
